bool is(struct TreeLinkNode* root, int parent, int pos, int flag)
{
    if (root == NULL)
        return true;
    if (root->left == root->right)
    {
        return true;
    }
    if (flag == 0)
    {
        if (root->left)
        {
            if (root->left->val >= root->val)
                return false;
        }
        if (root->right)
        {
            if (root->right->val <= root->val)
                return false;
        }
    }
    if (root->left && flag == 1)
    {
        if (pos == 0)
        {
            if (root->left->val >= root->val)
                return false;
        }
        if (pos == 1)
        {
            if (root->left->val >= root->val || root->left->val <= parent)
                return false;
        }
    }
    if (root->right && flag == 1)
    {
        if (pos == 0)
        {
            if (root->right->val <= root->val || root->right->val >= parent)
                return false;
        }
        if (pos == 1)
        {
            if (root->right->val <= root->val)
                return false;
        }
    }
    return is(root->left, root->val, 0, 1) && is(root->right, root->val, 1, 1);
}

bool isValidBST(struct TreeNode* root) {
    // write code here
    bool i = is(root, root->val, 0, 0);
    return i;

}